<?php

namespace App\Http\Controllers\Api\V2;

use Illuminate\Http\Request;
use App\Http\Requests\ApiV2\PartsRequest;
use App\Services\Warehouse\Interfaces\PartsServiceInterface;
use Illuminate\Support\Facades\DB;
use App\Services\Warehouse\Services\PartsService;
use App\Services\Warehouse\Model\Parts;
use App\Services\Warehouse\Model\Vendor;
use App\Services\Warehouse\Model\PartsCategory;
use App\Services\Warehouse\Model\Source;


class PartsController extends BaseController
{
    /**
     * @var Parts
     */
    protected $partsService;

    public function __construct(PartsServiceInterface $partsService)
    {
        $this->partsService = $partsService;
    }


    public function index(Request $request)
    {
        $vendor = $this->partsService->index($request->all());
        return $this->data($vendor);
    }

    public function create()
    {
        $source = Source::query()
            ->select([
                'id AS id',
                'name AS name',
                ])
            ->get();
        $vendors = Vendor::query()
            ->select([
                'id AS id',
                'vendorName AS name',
                ])
            ->get();
        $categorys = PartsCategory::query()
            ->select([
                'id AS id',
                'categoryName AS name',
                ])
            ->get();
        return $this->successData(compact('source','vendors','categorys'));
    }

    public function store(PartsRequest $request)
    {
        $this->partsService->set($request->filldata());
        return $this->success();
    }

    public function edit($id)
    {
        $vendor = $this->partsService->get($id);
        return $this->data($vendor);
    }

    public function update(PartsRequest $request, $id)
    {
        $this->partsService->set($request->filldata(), $id);
        return $this->success();
    }

    public function destroy($id)
    {
        $this->partsService->destroy($id);
        return $this->success();
    }

    public function information($partsCode){
        $parts = $this->partsService->information($partsCode);
        return $this->data($parts);
    }
}
